package com.nanhang.controller;

import com.nanhang.booksearch.BookIS0;
import com.nanhang.booksearch.ReaderS0;
import com.nanhang.entity.Bookborrow;
import com.nanhang.entity.Bookinfo;
import com.nanhang.entity.Reader;
import com.nanhang.service.IReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import util.PageResult;
import util.R;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymm
 * @since 2025-01-09
 */
@RestController
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private IReaderService readerService;

    @PostMapping("/search/{page}/{pagesize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pagesize") Integer pagesize,
                    @RequestBody(required = false) ReaderS0 RS0) {
        PageResult<Reader> res = readerService.search(page, pagesize, RS0);
        return R.ok().data("res", res);
    }

    @PostMapping("/add")
    public R add(@RequestBody Reader reader){
        return readerService.save(reader) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }

    @PutMapping("/update")
    public R update(@RequestBody Reader reader){
        return readerService.updateById(reader) ? R.ok().message("修改成功！") : R.error().message("修改失败！");
    }

    @DeleteMapping("/delete/{drid}")
    public R delete(@PathVariable("drid") String drid){
        String[] split = drid.split(",");
        List<String> drids = Arrays.asList(split);
        return readerService.removeByIds(drids) ? R.ok().message("删除成功"):R.error().message("删除失败！");
    }
}
